package com.aurora_manito.xiaomi.service.impl;

import com.aurora_manito.xiaomi.entity.Cart;
import com.aurora_manito.xiaomi.entity.Goods;
import com.aurora_manito.xiaomi.entity.GoodsType;
import com.aurora_manito.xiaomi.mapper.CartMapper;
import com.aurora_manito.xiaomi.mapper.GoodsMapper;
import com.aurora_manito.xiaomi.mapper.GoodsTypeMapper;
import com.aurora_manito.xiaomi.service.CartService;
import com.aurora_manito.xiaomi.service.GoodsService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Aurora_Manito
 * @since 2020-05-27
 */
@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements CartService {
   @Autowired
    private CartMapper cartMapper;
   @Autowired
   private GoodsService goodsService;

    private Map<String,Object> condition=new HashMap<>();
    //通过用户id，商品id 查询购物车信息2020 6 4
    @Override
    public Cart findByUidAndgid(int uid, int gid) {
        QueryWrapper<Cart> queryWrapper=new QueryWrapper<>();
        condition.put("userid",uid);
        condition.put("goods_id",gid);
        List<Cart> carts = cartMapper.selectByMap(condition);
        return carts!=null&&carts.size()==1?carts.get(0):null;
    }
    //添加购物车信息2020 6 4
    @Override
    public int add(Cart cart) {

        //INSERT INTO tb_cart  ( buy_count, goods_id, subtotal )  VALUES  ( ?, ?, ? )
        //# Cause: java.sql.SQLException: Field 'userid' doesn't have a default value
        //; Field 'userid' doesn't have a default value; nested exception is java.sql.SQLException: Field 'userid' doesn't have a default value
        //解决办法 更改实体注解
        return cartMapper.insert(cart);
    }
    //更新购物车信息2020 6 4
    @Override
    public int update(Cart cart)
    {
        condition.clear();
        condition.put("userid",cart.getUserid());
        condition.put("goods_id",cart.getGoodsId());
        return cartMapper.update(cart,new QueryWrapper<Cart>().allEq(condition));
    }

    //通过用户id查询购物车信息2020 6 4
    @Override
    public List<Cart> findByUid(Integer uid) {
        condition.clear();
        condition.put("userid",uid);
        List<Cart> carts = cartMapper.selectByMap(condition);
        if(carts!= null) {
            for (Cart cart : carts) {
                String goodsid=cart.getGoodsId()+"";
                Goods goods = goodsService.getGoodsById(goodsid);
                cart.setGoods(goods);
            }
            return carts;
        }
        return null;

    }

    //订单的删除
    @Override
    public int delete(int uid, int gid) {
        condition.clear();
        condition.put("userid",uid);
        condition.put("goods_id",gid);
        return cartMapper.deleteByMap(condition);
    }

    @Override
    public int deleteByUid(int uid) {
        condition.clear();
        condition.put("userid",uid);
        return cartMapper.deleteByMap(condition);
    }
}
